package com.ls.lserp.modules.purchase.service;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.CollectionsUtils;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.modules.material.entity.InventoryRequest;
import com.ls.lserp.modules.purchase.dao.PurchaseRequestDao;
import com.ls.lserp.modules.purchase.entity.PurchaseRequest;
import com.ls.lserp.modules.setting.dao.OgnizationDao;
import com.ls.lserp.modules.setting.entity.Ognization;

@Service
@Transactional(readOnly = false)
public class PurchaseRequestService extends CrudService<PurchaseRequestDao,PurchaseRequest>{

	    @Autowired
	    private PurchaseRequestDao purchaseRequestDao;
	    
	    @Autowired
	    private OgnizationDao ognizationDao;
	    
		/**
		 * @Title: findList
		 * @Description: 一览查询
		 * @param page
		 * @param purchaseRequest
		 * @return Page<PurchaseRequest>
		 */
		public Page<PurchaseRequest> findList(Page<PurchaseRequest> page, PurchaseRequest purchaseRequest) {
			
			// 入库申请明细
			List<PurchaseRequest> irDetailList = null;
			// 入库申请数量
			BigDecimal tempRequestQuantity = null;
			// 入库实际数量
			BigDecimal tempRealQuantity = null;
			// 未入库数量
			BigDecimal tempResidueQuantity = null;
			
			// tab页分，检索条件设定(0:全部,1:待入库,2:已入库)
			purchaseRequest.setInventoryStatus(ResourcesUtil.getConfig("inventory_status_wait"));
			
			// 入出库区分（入库)
			purchaseRequest.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_in"));
			
			// 入库类别（自采入库）
			purchaseRequest.setInventoryCategory(ResourcesUtil.getConfig("inventory_category_purchase"));
			
			// 设置分页参数
			purchaseRequest.setPage(page);
			List<PurchaseRequest> resultList = purchaseRequestDao.findList(purchaseRequest);
			
			if(!CollectionsUtils.isEmpty(resultList)){
				// 库存申请信息
				for(PurchaseRequest ir : resultList){
					tempRequestQuantity = new BigDecimal("0");
					tempRealQuantity = new BigDecimal("0");
					tempResidueQuantity = new BigDecimal("0");
					irDetailList = purchaseRequestDao.findInventoryRequestDetailList(ir);
					if(!CollectionsUtils.isEmpty(irDetailList)){
						// 库存申请明细
						for(PurchaseRequest ird : irDetailList){
							tempRequestQuantity = tempRequestQuantity.add(checkBigDecimalNull(ird.getRequestQuantity()));
							tempRealQuantity = tempRealQuantity.add(checkBigDecimalNull(ird.getRealQuantity()));
						}
						tempResidueQuantity = tempRequestQuantity.subtract(tempRealQuantity);
						ir.setRequestQuantity(tempRequestQuantity);
						ir.setRealQuantity(tempRealQuantity);
						ir.setResidueQuantity(tempResidueQuantity);
					}
				}
			}
			
	        // 执行分页查询
	        page.setList(resultList);
	        
	        return page;
		}
		
		/**
		 * check BigDecimal
		 * @param cost
		 * @return 取不到返回0
		 */
		public BigDecimal checkBigDecimalNull(BigDecimal cost){
			if(cost==null){
				return new BigDecimal("0");
			}
			return cost;
		}
		   /**
	     * @Title: selVenderList
	     * @Description: 查询
	     * @param @param ognization
	     * @param @return
	     * @return Page<ognization> 
	     * @throws
	     */
	   public List<Ognization> selVenderList(Ognization ognization) {
		   ognization.setPurchaseFlag("purchaseRequest");
	       return ognizationDao.findList(ognization);
	   }
	  
	  /**
	   * @Title: saveInventoryRequest
	   * @Description: 保存入库申请
	   * @param @param inventoryRequest
	   * @return void 
	   * @throws
	   */
	  @Transactional(readOnly = false)
	  public void saveInventoryRequest(PurchaseRequest purchaseRequest) {
	  	
	  	// 判断新建编辑
	  	//若为null 执行插入语句（新建）
	  	if (purchaseRequest.getId()==null){			
	  		purchaseRequest.preInsert();
	  		purchaseRequest.setVersion(1L);
	  		purchaseRequest.setDeleteFlag(InventoryRequest.DEL_FLAG_NORMAL);
	  		purchaseRequest.setEnableFlag(InventoryRequest.ENABlE_FLAG_NORMAL);
	  		purchaseRequest.setInventoryStatus(ResourcesUtil.getConfig("inventory_status_wait"));
	  		purchaseRequest.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_in"));
	  		purchaseRequestDao.insertInventoryRequest(purchaseRequest);
	  	}else{
			// 更新数据
	  		purchaseRequest.preUpdate();
	  		purchaseRequestDao.updateInventoryRequest(purchaseRequest);
			// 删除入库单明细
	  		purchaseRequestDao.deleteRequestDetails(purchaseRequest);
	  	}
	  	
	  	
	  	// 判断list是否为空
	  	if(!CollectionsUtils.isEmpty(purchaseRequest.getRequestDetailsList())){
	  	   // 遍历赋值
	         for(PurchaseRequest itr : purchaseRequest.getRequestDetailsList()){
	        	 itr.setId(purchaseRequest.getId());
	        	 itr.preInsert();
	        	 itr.setVersion(1L);
	        	 itr.setDeleteFlag(InventoryRequest.DEL_FLAG_NORMAL);
	        	 itr.setEnableFlag(InventoryRequest.ENABlE_FLAG_NORMAL);
	        	 itr.setStockinStatus(ResourcesUtil.getConfig("inventory_status_wait"));
	        	 purchaseRequestDao.insertInventoryRequestDetails(itr);
	        	 purchaseRequestDao.updateContractDetailStatus(itr);
	  		}
	  	}
	  }
	  
	  /**
	   * @Title: findRequestDetailsList
	   * @Description: 入库申请明细
	   * @param @param inventoryRequest
	   * @param @return
	   * @return List<InventoryRequest>
	   * @throws
	   */
	 
	 public List<PurchaseRequest> findRequestDetailsList(PurchaseRequest purchaseRequest) {
	     return purchaseRequestDao.findRequestDetailsList(purchaseRequest);
	 }
}
